﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cmsservice.Entity;
using cmsservice.DAL;
using cmsservice.Common;
using cmsservice.Common.Memcached;


namespace cmsservice.BLL
{
    public class VM_DMS_GetNewsByCodeBLL
    {
        /// <summary>
        /// 财经资讯
        /// </summary>
        /// <returns></returns>
        public List<VM_DMS_NewsList> GetCaiJingZiXun(int top)
        {
            VM_DMS_GetNewsByCodeDAL dal = new VM_DMS_GetNewsByCodeDAL();
            return dal.GetCaiJingZiXun(top);
        }

        /// <summary>
        /// 研究报告 标题
        /// </summary>
        /// <returns></returns>
        public List<VM_DMS_NewsList> GetYanJiuBaoGao(string code)
        {
            VM_DMS_GetNewsByCodeDAL dal = new VM_DMS_GetNewsByCodeDAL();
            return dal.GetYanJiuBaoGao(code);
        }

        /// <summary>
        /// vm_dms_getnewsbycode 新闻内容
        /// </summary>
        /// <returns></returns>
        public VM_DMS_GetNewsByCode GetNewsInfo(long id)
        {
            News2CacheTools cache = new News2CacheTools();
            string key = "cmsservice.BLL.VM_DMS_GetNewsByCodeBLL.GetNewsInfo."+id;
            var c= cache.Get<VM_DMS_GetNewsByCode>(key);
            if (c == null)
            {
                VM_DMS_GetNewsByCodeDAL dal = new VM_DMS_GetNewsByCodeDAL();
                 c = dal.GetNewsInfo(id);
                 if (c != null)
                 {
                     cache.Add(key, c, 2, 0, 0);
                 }
            }
            return c;
        }

        /// <summary>
        /// 券商晨会
        /// </summary>
        /// <returns></returns>
        public List<VM_DMS_GetNewsByCode> GetQianShangChengHuiList(int top)
        {
            VM_DMS_GetNewsByCodeDAL dal = new VM_DMS_GetNewsByCodeDAL();
            return dal.GetQianShangChengHuiList(top);
        }

        /// <summary>
        /// 股市日历
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public List<VM_DMS_GetNewsByCode> GetGuShiRiLiList(DateTime sDate)
        {
            List<VM_DMS_GetNewsByCode> list = new List<VM_DMS_GetNewsByCode>();
            string key = "GetGuShiRiLiList";
            object o = Cache.Get(key);
            if (o == null)
            {
                try
                {
                    VM_DMS_GetNewsByCodeDAL dal = new VM_DMS_GetNewsByCodeDAL();
                    list = dal.GetGuShiRiLiList();
                }
                catch
                {
                }
                if (list != null && list.Count > 0)
                    Cache.Add(key, list, DateTime.Now.AddMinutes(5));
            }
            else
            {
                list = o as List<VM_DMS_GetNewsByCode>;
            }
            DateTime eDate = sDate.AddDays(1);
            if (list != null)
                return list.Where(x => x.PTime >= sDate && x.PTime < eDate).ToList();
            else
                return null;
        }
             /// <summary>
        /// 根据code获取新闻
        /// </summary>
        /// <param name="top">返回条数</param>
        /// <param name="code">新闻类型</param>
        /// <returns></returns>
        public List<NewsList> GetNewsByCode(int top, string code)
        {
            VM_DMS_GetNewsByCodeDAL vm = new VM_DMS_GetNewsByCodeDAL();
            return vm.GetNewsByCode(top,code);
        }
        public string NewsByCodeList(string typeName, string code, int count = 500)
        {
            string key = "cmsservice.BLL.NewsListBLL.NewsByCodeList" + typeName + count.ToString() + code;
             key= MD5Hash.getMd5Hash(key);
            News2CacheTools cache = new News2CacheTools();
            string str = "";//cache.Get<string>( key);
            if (string.IsNullOrEmpty(str))
            {
                VM_DMS_GetNewsByCodeBLL vm = new VM_DMS_GetNewsByCodeBLL();
                var lst = vm.GetNewsByCode(count, code);
                //for (int i=0;i<lst.Count;i++)
                //{
                //    if (i == 0)
                //    {
                //        lst[i].Step = 6;
                //    }
                //    else {
                //        lst[i].Step = 5;
                //    }
                //}
                
                var zData = new NewsList_Lst { typename = typeName, typeid = code, list = lst };
                str = Newtonsoft.Json.JsonConvert.SerializeObject(zData);
                cache.Add(key, str, 0, 10, 0);
            }
       
            return str;
        }

        public string GetNewsByStock(string typeName, string code, string stock, int count = 500)
        {
            string key = "cmsservice.BLL.NewsListBLL.GetNewsByStock" + typeName + count.ToString() + code+stock;
            key = MD5Hash.getMd5Hash(key);
            News2CacheTools cache = new News2CacheTools();
            string str = cache.Get<string>(key);
            if (string.IsNullOrEmpty(str))
            {
                VM_DMS_GetNewsByCodeDAL vm = new VM_DMS_GetNewsByCodeDAL();
                var lst = vm.GetNewsByStock(count, code,stock);
                //for (int i = 0; i < lst.Count; i++)
                //{
                //    if (i == 0)
                //    {
                //        lst[i].Step = 6;
                //    }
                //    else
                //    {
                //        lst[i].Step = 5;
                //    }
                //}

                var zData = lst;
                str = Newtonsoft.Json.JsonConvert.SerializeObject(zData);
                cache.Add(key, str, 1, 0, 0);
            }

            return str;
        }
       
    }
}
